package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.util.MovieRecorder;

public class VideoRecoderActivity extends BaseToolBarActivity {
    private ToggleButton mLuxiang;
    private TextView nextBtn;
    private SurfaceView videoSureface;
    private MovieRecorder movieRecorder;
    private String filePath;
    private TextView tvTime;
    private Team team;
    private int SET_TIME = 7;

    private Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == SET_TIME) {
                tvTime.setText(msg.obj + "");
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_recoder);
        team = (Team) getIntent().getSerializableExtra("team");
        setToolbarTitle("录像");
        nextBtn = setRightBtn("下一步");
        initWidget();
        movieRecorder = new MovieRecorder(this, new MovieRecorder.TimeListener() {
            @Override
            public void time(int seconds) {
                Message message = new Message();
                message.what = SET_TIME;
                message.obj = seconds + "";
                handle.sendMessage(message);
            }
        },videoSureface);
        luxiangListener();
    }

    @Override
    public void initWidget() {
        tvTime = (TextView) findViewById(R.id.tv_time);
        mLuxiang = (ToggleButton) findViewById(R.id.tb_luxiang);
        videoSureface = (SurfaceView) findViewById(R.id.video_surface);
    }

    public void luxiangListener() {
        mLuxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mLuxiang.isChecked()) {
                    start();
                } else {
                    filePath=save();
                }
            }
        });
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forwardNext();
            }
        });
    }

    public void start() {
        movieRecorder.startRecording(videoSureface);
    }

    public void pause() {
        movieRecorder.stop();

    }

    public String save() {
        return movieRecorder.stopRecording();
    }

    public void forwardNext() {
        if(!mLuxiang.isChecked())
        {
            toastMsg("请先停止录像!");
        }
        Intent intent = new Intent(this, VideoNextActivity.class);
        intent.putExtra("videopath", filePath);
        intent.putExtra("teamid",team.teamid);
        startActivity(intent);


    }


    @Override
    public void initListener() {

    }


}
